package com.redlion.object;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.util.modifier.ease.EaseLinear;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.redlion.Game;
import com.redlion.object.Planet;

public class AnimatedImpact extends AbstractAnimationObject {
	private static final String TAG = "AnimatedImpact";
	public static final int FRAME_DURATION = 100;
	public IEntityModifier rotation;

	public AnimatedImpact(Vector2 position, ITiledTextureRegion textureRegion, FixtureDef fixtureDef) {
		super(position, textureRegion, fixtureDef);
	}

	@Override
	protected void createAnimation() {
	}

	@Override
	protected void createAnimation(float px, float py) {
		sprite = new AnimatedSprite(px, py, mTiledTextureRegion, Game.gameActivity.getVertexBufferObjectManager());
	}

	@Override
	protected void createBody(FixtureDef fixtureDef) {
	}

	@Override
	protected void update(float delta) {

	}

	@Override
	protected IAnimationListener getAnimationListener() {
		return new IAnimationListener() {

			@Override
			public void onAnimationStarted(AnimatedSprite pAnimatedSprite, int pInitialLoopCount) {
			}

			@Override
			public void onAnimationLoopFinished(AnimatedSprite pAnimatedSprite, int pRemainingLoopCount, int pInitialLoopCount) {
			}

			@Override
			public void onAnimationFrameChanged(AnimatedSprite pAnimatedSprite, int pOldFrameIndex, int pNewFrameIndex) {
			}

			@Override
			public void onAnimationFinished(AnimatedSprite pAnimatedSprite) {
				// userData(GameController.REMOVE);
			}
		};

	}

	public void enableRotation(float duration) {
		rotation = new RotationModifier(duration, sprite.getRotation(), sprite.getRotation() + 360, EaseLinear.getInstance()) {
			@Override
			protected void onSetValue(final IEntity pEntity, final float pPercentageDone, final float pRotation) {
				if (pPercentageDone == 1) {
					this.reset();
				}
				super.onSetValue(pEntity, pPercentageDone, pRotation);
			}
		};
		sprite.registerEntityModifier(rotation);
	}

	public void enableRotation(IEntityModifier rotation) {
		sprite.registerEntityModifier(rotation);
	}

	public void activate() {
		sprite.setIgnoreUpdate(false);
		sprite.setVisible(true);
		animate(FRAME_DURATION, 0);
		userData(getClass());
	}

	public void deactivate() {
		sprite.setIgnoreUpdate(true);
		sprite.setVisible(false);
		userData(null);
	}

	public Object userData() {
		return sprite.getUserData();
	}

	public void userData(Object o) {
		sprite.setUserData(o);
	}
}
